﻿using System;
using System.Timers;
using Wise.TimeWarrior.DTO;
using Wise.TimeWarrior.Interfaces;
using Wise.TimeWarrior.OSFunctionalitiesProxy;

namespace Wise.TimeWarrior.Business.Impl
{
    /// <summary>
    /// <see cref="IUserLoggingBusinessLogic"/>
    /// </summary>
    public class UserLoggingBusinessLogic : IUserLoggingBusinessLogic, IDisposable
    {
        /// <summary>
        /// <see cref="IUserLoggingBusinessLogic.DataStoreService"/>
        /// </summary>
        public IDataStore DataStoreService { set; private get; }

        /// <summary>
        /// <see cref="IUserLoggingBusinessLogic.SystemUsageService"/>
        /// </summary>
        public ISystemUsage SystemUsageService { set; private get; }

        /// <summary>
        /// <see cref="IUserLoggingBusinessLogic.SystemTimer"/>
        /// </summary>
        public ISystemTimer SystemTimer { set; private get; }

        /// <summary>
        /// <see cref="IUserLoggingBusinessLogic.DateTimeProvider"/>
        /// </summary>
        public IDateTimeProvider DateTimeProvider { set; private get; }

        /// <summary>
        /// <see cref="IUserLoggingBusinessLogic.CheckInterval"/>
        /// </summary>
        public double CheckInterval { set; private get; }

        /// <summary>
        /// <see cref="IUserLoggingBusinessLogic.Configuration"/>
        /// </summary>
        public IConfiguration Configuration { set; private get; }


        /// <summary>
        /// event fired after every Time interval described in <see cref="CheckInterval"/>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SystemTimer_ElapsedTime(object sender, ElapsedEventArgs e)
        {
            StoreData();
        }

        /// <summary>
        /// method stores data when tehere are any Events not saved
        /// </summary>
        private void StoreData()
        {
            if (SystemUsageService.Events.Count > 0)
            {
                DataStoreService.SaveData(SystemUsageService.Events);
                SystemUsageService.ClearEventData();
            }
        }

        /// <summary>
        /// <see cref="IDisposable.Dispose"/>
        /// </summary>
        public void Dispose()
        {
            SystemUsageService.StopWatchForUserStatus();
            StoreData();
            SystemTimer.ElapsedTime -= SystemTimer_ElapsedTime;
            SystemTimer.Stop();
        }
        /// <summary>
        /// <see cref="IBLSetup.SetupLogic"/>
        /// </summary>
        public void SetupLogic()
        {
            ConfigurationDto configuration = Configuration.ReadConfiguration();
            DataStoreService.DataStorePath = configuration.PathToDataStore;
        }

        /// <summary>
        /// <see cref="IUserLoggingBusinessLogic.CollectData"/>
        /// </summary>
        /// <returns></returns>
        public BusinessItemDto CollectData()
        {
            
            BusinessItemDto miDto = new BusinessItemDto();
            miDto.ActionOption = BusinessItemOption.AutoRunHidden;

            miDto.Action = () =>
            {
                SystemUsageService.StartWatchForUserStatus();
                SystemTimer.Interval = CheckInterval;
                SystemTimer.ElapsedTime += SystemTimer_ElapsedTime;
                SystemTimer.Start();
            };

            return miDto;
        }
    }
}
